<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 卡片区域 -->
      <el-card v-loading="loading" class="tree-card">
        <!-- 公司名字 -->
        <tree-tools :node-data="company" :is-root="true" @add-detps="handleAddDepts" />
        <!-- 部门 el-tree
        data:要展示的树状数据
        props：设置树状数据的展示名
        default-expand-all:树状数据设置展开 -->
        <el-tree :data="list" :props="defaultProps" default-expand-all>
          <template v-slot="{data}">
            <!-- 自定义属性向子组件传值：如果有多个单词，单词与单词之间'-'隔开
          在子组件内部用小驼峰命名接收 -->
            <tree-tools
              :node-data="data"
              @del-detps="reqGetDepartments"
              @add-detps="handleAddDepts"
              @edit-detps="editDepts"
            />
          </template>
        </el-tree>
      </el-card>

      <!-- 新增弹出框   show-dialog.sync修饰符-->
      <AddDepts
        ref="editRef"
        :depts-list="deptsList"
        :node-data="nodeData"
        :show-dialog.sync="showDialog"
        @add-success="reqGetDepartments"
      />
    </div>
  </div>
</template>

<script>
import { tranListToTree } from '@/utils/index' // 导入封装的函数 把列表型数据转换为树状型数据
import { reqDepartments, reqGetDepartDetail } from '@/api/departments' // 导入axios
import treeTools from './components/tree-tools.vue' // 导入注册子组件
import AddDepts from './components/add-depts.vue'
export default {
  name: 'Departments',
  components: { treeTools, AddDepts }, // 注册组件
  data() {
    return {
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      company: {}, // 标题头部
      showDialog: false, // 弹出框的默认值
      nodeData: {}, // 储存点击部门时的数据

      deptsList: [], // 把res.depts原数据传给子组件做判断

      loading: false // 页面加载效果
    }
  },
  // created钩子函数里可以直接调用methods里的函数
  created() {
    this.reqGetDepartments()
  },
  methods: {
    // 1.获取企业的部门列表
    async reqGetDepartments() {
      this.loading = true // 加载效果开启
      const { data: res } = await reqDepartments()
      this.company = { name: res.companyName, manager: '负责人', id: '' }
      this.deptsList = res.depts
      this.list = tranListToTree(res.depts, '')
      this.loading = false // 加载效果关闭
    },
    // 2.点击添加子部门弹出窗口,并且接收点击当前部门的数据
    handleAddDepts(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      this.$refs.editRef.showTitle = '新增部门'
    },
    // 3.编辑
    async editDepts(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      // this.$nextTick(() => {
      //   this.$refs.editRef.getDepartDetail()
      // })
      // 调用axios函数接收数据
      const res = await reqGetDepartDetail(this.nodeData.id)
      this.$refs.editRef.form = res.data
      this.$refs.editRef.showTitle = '编辑部门'
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
